<?php
/**
 * Created by PhpStorm.
 * User: jiangtaiping
 * Date: 2017/2/23
 * Time: 14:22
 */
class Controller_Adminmall_Auth extends FController {

    private $DBServer = "xhs_slave";
    /**
     * 用户登录界面
     */
    public function loginAction() {
//        $this->openDebug();
        if ($this->isPost()) {
            $username = trim($_POST['username']);
            $password = trim($_POST['password']);

            $checkCode = FRequest::getPostString('check_code');

            if (!$checkCode) {
                $this->error('请输入验证码！');
            }
            session_start();
            if ($checkCode != $_SESSION['rand_code']) {
                $this->error('验证码错误！');
            }
            $refer = trim($_POST['refer']);
            if (strpos($refer, 'login')) {
                $refer = null;
            }

            $managerTable = new FTable('manager','','xhs_slave');
            $encryptPassword = Service_ManagerMall::getEncryptPassword($password);
            $managerData = $managerTable->where(array('username' => $username))->find();

            if (!$managerData) {
                Service_ManagerMall::addManagerLog('login_error','用户名不存在:'.$username);
                $this->error('用户名不存在！');
            } else {

                if ($managerData['password'] == $encryptPassword) {
                    $auth_str = md5("{$managerData['username']}|{$managerData['password']}|{$managerData['groupid']}");
                    // 更新登录时间
                    $managerTable = new FTable('manager','','xhs_master');
                    $managerTable->where(array("uid" => $managerData['uid']))->update1(array('last_login_time' => time()));
                    Service_ManagerMall::addManagerLog('login_success','账号登录成功:'.$username);
                    FCookie::set('manager_auth', "{$managerData['uid']}\t{$auth_str}", 3600000);
//                    var_dump(2222);exit();
                    FResponse::redirect('/');
                } else {
                    Service_ManagerMall::addManagerLog('login_pwd_error','密码错误:'.$username);
                    $this->error('对不起，密码错误！');
                }
            }
        }else{
            $this->display('adminmall/login');
        }
    }

    /**
     * 退出登录
     */
    public function logoutAction() {
        Service_ManagerMall::removeSession();
        FResponse::redirect('/');
    }

    /**
     * 显示提示信息
     * @param string $message
     * @param string $messageType
     * @param null $jumpUrl
     */
    function showMessage($message, $messageType = 'success', $jumpUrl = null) {
        $this->assign('messageType', $messageType);
        $this->assign('message_content', $message);
        $this->assign('jump_url', $jumpUrl);
        $this->display('adminmall/message');
        exit(0);
    }

    /**
     * 显示并输出模板
     * @param null $tpl
     */
    function display($tpl = null) {
        parent::display(str_replace('adminmall/', '', $tpl));
    }

    public function testAction(){
        Service_ManagerMall::clearGroupRole(2);
        Service_ManagerMall::clearGroupRole(3);
    }
}
